// ============================== 导入依赖 ============================== //

import CORE from '@/CORE'
import Client from '@api/questionnaire/api/IbQuestionnaireMobileTerminalApi'

import InfoClass from '@api/questionnaire/model/QuestionnaireMobileterminalReq'
import SubmitClass from '@api/questionnaire/model/QuestionnaireSubmitMobileterminalReq'
import ChildClass from '@api/questionnaire/model/QuestionnaireSubmitDto'

const OneClass = InfoClass

const CLIENT = CORE.grpcCLIENT(Client)
const CHILDREN = [ChildClass, 'dtos', 1]

// ============================== 导出远程接口 ============================== //

export default {
  // —————————————————————————————— 信息 —————————————————————————————— //

  async getInfo({ instanceId }, TIP = '查询信息') {
    const DTO = CORE.grpcDTO(InfoClass, { instanceId })
    const resp = await CORE.grpc(TIP, CLIENT, 'mobileterminalQuestionnaireDetail', DTO)
    return resp.dto || {}
  },

  // —————————————————————————————— 单项 —————————————————————————————— //

  async getOne({ instanceId }, TIP = '查询单项') {
    if (!instanceId) return {}

    const DTO = CORE.grpcDTO(OneClass, { instanceId })
    const { dtos = [] } = await CORE.grpc(TIP, CLIENT, 'mobileterminalQuestionnaireAnswer', DTO)
    return dtos[0] || {}
  },

  // —————————————————————————————— 提交 —————————————————————————————— //

  async submit(one, TIP = '提交') {
    const DTO = CORE.grpcDTO(SubmitClass, one, CHILDREN)
    await CORE.grpc(TIP, CLIENT, 'mobileterminalQuestionnaireSubmit', DTO)
    return CORE.grpcOK(TIP)
  },
}
